﻿using System;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Common.Locator;

using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.BizFacade
{
    public static class TrackingEntityObjectMediator
    {
        public static ICmsContext ApplicationContext
        {
            get
            {
                return DependencyServiceLocator.Current.GetInstance<ICmsContext>();
            }
        }

        public static void TrackEntity(ITrackingEntityObject entity)
        {
            if (entity != null)
            {
                var user = ApplicationContext.ContextUser;

                Guard.ArgumentNotNull(user, "Context user");

                if (entity.IsNew)
                {
                    entity.CreatedBy = user.Id;
                    entity.DateCreated = DateTime.Now;
                }
                else
                {
                    entity.LastUpdatedBy = user.Id;
                    entity.DateLastUpdated = DateTime.Now;
                }
            }
        }
    }
}
